package cn.com.surker.system.service.impl;

import cn.com.surker.common.Constant;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.dao.RoleExamineLevelDao;
import cn.com.surker.system.entity.SysRoleExamineLevel;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.RoleExamineLevelService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* <p>
    * 配置某一角色在不同功能上的审核等级 服务实现类
    * </p>
*
* @author admin
* @since 2022-06-27
*/
@Service
public class RoleExamineLevelServiceImpl extends ServiceImpl<RoleExamineLevelDao, SysRoleExamineLevel> implements RoleExamineLevelService {

        @Resource
        private RoleExamineLevelDao mapper;


        /**
        * 根据条件查询所有的数据
        * @param dto
        * @return
        */
        public RestResultDto findAll(SysRoleExamineLevel dto){

            QueryWrapper<SysRoleExamineLevel> query = new QueryWrapper();
            query.setEntity(dto);
            List<SysRoleExamineLevel> list = mapper.selectList(query);

            return RestResultDto.success(list);
        }


        /**
        * 分页查询数据
        * @param dto page
        * @return
        */
        public RestResultDto findAllByPage(SysRoleExamineLevel dto, Page<SysRoleExamineLevel> page){
            QueryWrapper<SysRoleExamineLevel> query = new QueryWrapper();
            query.setEntity(dto);
            IPage<SysRoleExamineLevel> iPage = mapper.selectPage(page,query);

            return RestResultDto.success(iPage);
        }


        /**
        * 新增数据
        * @param dto page
        * @return
        */
        public RestResultDto saveInit(SysRoleExamineLevel dto, CacheUser user){
        	dto.setCreater(user.getId().toString());
        	dto.setCreateTime(new Date());
            int saveStatus = mapper.insert(dto);
            if(saveStatus > 0){
                return RestResultDto.success("保存成功!");
            }else{
                return RestResultDto.success("保存失败!");
            }

        }

        /**
        * 更新数据
        * @param dto page
        * @return
        */
        public RestResultDto update(SysRoleExamineLevel dto){

            int saveStatus = mapper.updateById(dto);
            if(saveStatus > 0){
                return RestResultDto.success("保存成功!");
            }else{
                return RestResultDto.success("保存失败!");
            }

        }

	@Override
	public RestResultDto queryData(Integer offset, Integer limit) {
		PageHelper.offsetPage(offset,limit);
		QueryWrapper<SysRoleExamineLevel> query = new QueryWrapper<>();
		query.eq("status",0);
		Page<SysRoleExamineLevel> iPage = mapper.selectPage(new Page<>((offset + limit) / limit, limit), query);
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal())){
				iPage.setTotal(pageInfo.getTotal());
			}
		}
		return RestResultDto.success(iPage);
	}

	@Override
	public SysRoleExamineLevel userLevel(CacheUser user) {
		/*SysRoleExamineLevel examinLevel = null;
		List<String> roleNameList = user.getRoleNameList();
		// 获取角色审核等级
		QueryWrapper<SysRoleExamineLevel> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", );
		queryWrapper.eq("status",0);
		queryWrapper.eq("org_id",user.getOriginId());
		queryWrapper.in("role_name",roleNameList);
		List<SysRoleExamineLevel> examineLevelList = mapper.selectList(queryWrapper);
		if(examineLevelList != null && !examineLevelList.isEmpty()){
			examinLevel = examineLevelList.get(0);
		}
		return examinLevel;*/
		return userLevel(user,Constant.EXAMINE_TYPE_WECHAT_ORDER);
	}

	@Override
	public SysRoleExamineLevel userLevel(CacheUser user,Integer type) {
		SysRoleExamineLevel examinLevel = null;
		List<String> roleNameList = user.getRoleNameList();
		// 获取角色审核等级
		QueryWrapper<SysRoleExamineLevel> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", type);
		queryWrapper.eq("status",0);
		queryWrapper.eq("org_id",user.getOriginId());
		queryWrapper.in("role_name",roleNameList);
		List<SysRoleExamineLevel> examineLevelList = mapper.selectList(queryWrapper);
		if(examineLevelList != null && !examineLevelList.isEmpty()){
			examinLevel = examineLevelList.get(0);
		}
		return examinLevel;
	}
}